﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BusinessLoginLayer
{
    public partial class Bookings
    {
        public static Guid NewBooking(string Token, string FlightID, string CreditCardNumber, string ClientNameOnInvoice, string ClientDocumentID, int InvoiceID, string InvoiceLink)
        {
            using (PlanesManagerEntity entity = new PlanesManagerEntity())
            {
                string userID = (from ent in entity.Company_Details
                                 where ent.AcessToken == Token
                                 select ent.id).FirstOrDefault();

                Guid bookingID = Guid.NewGuid();
                entity.Bookings.Add(new Bookings()
                {
                    BookingID = bookingID,
                    Client_ID = userID,
                    Flight_ID = FlightID,
                    CreditCardNumber = CreditCardNumber,
                    ClientNameOnInvoice = ClientNameOnInvoice,
                    ClientDocumentID = ClientDocumentID,
                    InvoiceID = InvoiceID.ToString(),
                    InvoiceURL = InvoiceLink,
                });
                entity.SaveChanges();

                return bookingID;
            }
        }

        public static CommonClasses.Booking GetBooking(string Id)
        {
            using (PlanesManagerEntity entity = new PlanesManagerEntity())
            {
                Guid IdParsed;
                if (!Guid.TryParse(Id, out IdParsed))
                    return null;

                return (from ent in entity.Bookings
                        where ent.BookingID == IdParsed
                        select new CommonClasses.Booking()
                        {
                            BookingID = IdParsed,
                            ClientDocumentID = ent.ClientDocumentID,
                            ClientNameonInvoice = ent.ClientNameOnInvoice,
                            FlightID = ent.Flight_ID,
                            InvoiceID = ent.InvoiceID,
                            InvoiceURL = ent.InvoiceURL,
                        }).FirstOrDefault();
            }
        }
    }
}
